package com.hg.library.widget;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.os.SystemClock;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.hg.highglass_library.R;

/**
 * Created by ASUS-PC on 2016/7/7.
 */
public class HiToast extends Toast {

    /**
     * Construct an empty Toast object.  You must call {@link #setView} before you
     * can call {@link #show}.
     *
     * @param context The context to use.  Usually your {@link Application}
     *                or {@link Activity} object.
     */
    public HiToast(Context context) {
        super(context);
    }

    public static final Toast showMessage(Context context, CharSequence message, final int duration) {
        Toast toast = getToast(context, duration);
        final View view = toast.getView();
        TextView textView = (TextView) view.findViewById(R.id.message);
        textView.setText(message);
        toast.show();

        return toast;
    }

    public static final Toast showMessage(Context context, CharSequence title, CharSequence message,CharSequence time, int duration) {
        Toast toast = getToast(context, duration);
        View view = toast.getView();

        TextView textView = (TextView) view.findViewById(R.id.title);
        textView.setText(title);

        TextView textView1 = (TextView) view.findViewById(R.id.message1);
        textView1.setText(message);

        final TextView textView2 = (TextView) view.findViewById(R.id.seconds);
        textView2.setText(time);
        toast.show();
        return toast;
    }

    public static final Toast showMessage(Context context, CharSequence title, CharSequence message, int duration, final OnHiToastDisplayListener listener) {
        Toast toast = getToast(context, Toast.LENGTH_LONG);
        View view = toast.getView();

        TextView textView = (TextView) view.findViewById(R.id.title);
        textView.setText(title);

        TextView textView1 = (TextView) view.findViewById(R.id.message1);
        textView1.setText(message);

        final TextView textView2 = (TextView) view.findViewById(R.id.seconds);

        toast.show();

        if (listener != null){
            new Thread(new Runnable() {
                @Override
                public void run() {
                    int i = 0;
                    textView2.setText(3 + "s");
                    while (i < 4) {
                        SystemClock.sleep(1_000);//因为先睡了一秒，当0秒走完回调，所以<4 如果<3 就是一到0秒，马上回调
                        i++;
                        final int finalI = i;
                        textView2.post(new Runnable() {
                            @Override
                            public void run() {
                                textView2.setText(3 - finalI + "s");
                            }
                        });
                    }
                    listener.onFinish();
                }
            }).start();
        }

        return toast;
    }

    private static Toast toast;

    private static final Toast getToast(Context context, int duration) {
        if (toast == null){
            toast = new Toast(context);
        }

        View view = View.inflate(context, R.layout.toast_layout, null);
        toast.setView(view);
        toast.setDuration(duration);
        return toast;
    }

    public interface OnHiToastDisplayListener{
        void onFinish();
    }

}
